# # coding=utf-8
# import logging
#
# from flask import jsonify
# from qiniu import Auth,put_data
#
# from api_v1.utils.response_code import RET
#
#
# def put_qiniu(f1):
#     access_key = 'dCGm3BuvU_Fg3WpkB_GOfHxbrABCpJDmVWeeVbMg'
#     secret_key = 'p-wtRhn18dQQk8N3dxOli3WEbNcfuPDcdDQ6YTpe'
#     # 空间名称
#     bucket_name = 'info11'
#     try:
#         # 构建鉴权对象
#         q = Auth(access_key, secret_key)
#         # 生成上传 Token
#         token = q.upload_token(bucket_name)
#         # 上传文件数据，ret是字典，键为hash、key，值为新文件名，info是response对象
#         ret, info = put_data(token, None, f1.read())
#         return ret.get('key')
#     except:
#         logging.ERROR(u'访问七牛云出错')
#         return jsonify(code=RET.SERVERERR)


from qiniu import Auth, put_file, etag
import qiniu.config

# 需要填写你的 Access Key 和 Secret Key
from qiniu import put_data

access_key = 'dCGm3BuvU_Fg3WpkB_GOfHxbrABCpJDmVWeeVbMg'
secret_key = 'p-wtRhn18dQQk8N3dxOli3WEbNcfuPDcdDQ6YTpe'


def put_qiniu(image_data):
    # 构建鉴权对象
    q = Auth(access_key, secret_key)

    # 要上传的空间
    bucket_name = 'info11'

    # 上传后保存的文件名
    # 上传后保存的文件名,如果自己去维护比较蛮烦,如果设置为None那么就交给七牛云维护
    key = None

    # 生成上传 Token，可以指定过期时间等
    token = q.upload_token(bucket_name, key, 3600)

    # 要上传文件的本地路径
    # localfile = './aaa.jpg'
    ret, info = put_data(token, key, image_data.read())
    # print(info)
    # print(ret)
    # assert ret['key'] == key
    # assert ret['hash'] == etag(localfile)
    # 如果上传成功之后返回图片名字
    if info.status_code == 200:
        return ret.get("key")
    else:
        return None
#
# if __name__ == '__main__':
#     with open('./aaa.jpg', 'rb') as f:
#         image_name = image_storage(f.read())
#         print(image_name)
